package com.yuqing.sentimentapp.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;

import com.linkface.baselibrary.utils.DensityUtil;
import com.linkface.baselibrary.utils.LogUtil;
import com.linkface.common.adaper.CommonViewHolder;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;
import com.yuqing.sentimentapp.home.acitivity.NewsDetailActivity;
import com.yuqing.sentimentapp.home.acitivity.SpecialTopicDetailActivity;
import com.yuqing.sentimentapp.home.adapter.NewsAdapter;
import com.yuqing.sentimentapp.home.bean.NewsDataBean;
import com.yuqing.sentimentapp.home.bean.RecommendDataBean;
import com.yuqing.sentimentapp.home.presenter.NewsPresenter;
import com.yuqing.sentimentapp.home.presenter.impl.NewsPresenterImpl;
import com.yuqing.sentimentapp.home.view.NewsView;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.widget.RecycleViewDivider;
import com.yuqing.sentimentapp.widget.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 新闻页
 * Created by ShenHe on 2019/4/11.
 */

public class NewsFragment extends BaseFragment implements NewsView<NewsDataBean> {

    private static final String ARG_PARAM = "arg_param";
    @BindView(R.id.recyclerView)
    XRecyclerView mRecyclerView;
    private NewsAdapter mAdapter;
    private List<NewsDataBean.DataBean> dataList = new ArrayList<>();
    private String category;
    private NewsPresenter mPresenter;
    private int page = 1;
    private boolean isStartLoad;//是否是第一次加载数据标志  解决嵌套fragment懒加载失效问题
    private boolean isVip;

    public NewsFragment() {

    }

    public static NewsFragment newInstance(String param) {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM, param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initIntentData();
        initRecyclerView();
    }

    private void loadData(boolean isRefresh) {
        String phone = SharePrefrenceHelper.getLoginMobile();
        if (TextUtils.equals("T", category)) {
            if (!isStartLoad) {
                mPresenter.loadSpecialTopicData(phone,page, 10, isRefresh);
            } else {
                if(getParentFragment().getUserVisibleHint()) {
                    mPresenter.loadSpecialTopicData(phone, page, 10, isRefresh);
                }
            }
        } else if (TextUtils.equals("F", category)) {
            if (!isStartLoad) {
                mPresenter.loadTopicAnalysisData(phone, page, 10, isRefresh);
            } else {
                if (getParentFragment().getUserVisibleHint()) {
                    mPresenter.loadTopicAnalysisData(phone, page, 10, isRefresh);
                }
            }
        } else {
            mPresenter.loadNewsData(phone, category, page, 10, isRefresh);
        }
        isStartLoad = true;
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            loadData(true);
        }
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);

        CommonViewHolder.onItemCommonClickListener commonClickListener = new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                if (isVip && !SharePrefrenceHelper.getIsVip()) {//是专题分析且不是VIP，弹提示
                    ToastUtil.showShort("您没有查看权限，请联系管理员");
                    return;
                }

                NewsDataBean.DataBean dataBean = dataList.get(position - 1);
                if (TextUtils.equals("T", category)) {
                    SpecialTopicDetailActivity.start(getActivity(), dataBean.getId(), dataBean.getTitle());
                } else if (TextUtils.equals("F", category)) {
                    NewsDetailActivity.start(getActivity(), dataBean.getId(), dataBean.getTitle(), true, true);
                } else {
                    NewsDetailActivity.start(getActivity(), dataBean.getId(), dataBean.getTitle(), false);
                }
                dataList.get(position - 1).setIsRead("1");
                mAdapter.notifyItemChanged(position);
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        };
        isVip = TextUtils.equals("F", category);//专题分析显示VIP标志
        mAdapter = new NewsAdapter(getActivity(), dataList, commonClickListener, isVip);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData(true);
            }

            @Override
            public void onLoadMore() {
                page++;
                loadData(false);
            }
        });
    }

    @Override
    protected void initPresenter() {
        mPresenter = new NewsPresenterImpl(this);
    }

    private void initIntentData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            category = arguments.getString(ARG_PARAM);
        }
    }

    @Override
    public void returnNewsResult(NewsDataBean result, boolean isRefresh) {
        stopLoad();
        if (null == result || result.getData().size() == 0) {
            if (!isRefresh) {
                mRecyclerView.setNoMore(true);
            }
            return;
        }
        if (isRefresh) {
            dataList.clear();
        }
        dataList.addAll(result.getData());
        mAdapter.notifyDataSetChanged();
    }

    private void stopLoad() {
        if (mRecyclerView != null) {
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();
        }
    }

    @Override
    public void onSuccess(NewsDataBean data) {

    }

    @Override
    public void onFailure(String message) {
        stopLoad();
        ToastUtil.showShort(message);
    }
}
